
// 暴力解法
; (function () {
    //return
    /**
   * Definition for singly-linked list.
   * function ListNode(val) {
   *     this.val = val;
   *     this.next = null;
   * }
   */

    /**
     * @param {ListNode} headA
     * @param {ListNode} headB
     * @return {ListNode}
     */
    var getIntersectionNode = function (headA, headB) {
        if (!headA || !headB) return null;

        let pA = headA;
        while (pA) {
            let pB = headB;

            while (pB) {
                if (pA === pB) return pA;
                pB = pB.next;
            }

            pA = pA.next;
        }
    };
})();

// 哈希表
; (function () {
    //return
    /**
   * Definition for singly-linked list.
   * function ListNode(val) {
   *     this.val = val;
   *     this.next = null;
   * }
   */

    /**
     * @param {ListNode} headA
     * @param {ListNode} headB
     * @return {ListNode}
     */
    var getIntersectionNode = function (headA, headB) {
        if (!headA || !headB) return null;

        const hashmap = new Map();

        let pA = headA;
        while (pA) {
            hashmap.set(pA, 1);
            pA = pA.next;
        }

        let pB = headB;
        while (pB) {
            if (hashmap.has(pB)) return pB;
            pB = pB.next;
        }
    };

    
})();

// 双指针
; (function () {
    //return
    /**
   * Definition for singly-linked list.
   * function ListNode(val) {
   *     this.val = val;
   *     this.next = null;
   * }
   */

    /**
     * @param {ListNode} headA
     * @param {ListNode} headB
     * @return {ListNode}
     */
    var getIntersectionNode = function (headA, headB) {
        if (!headA || !headB) return null;

        let pA = headA,
            pB = headB;
        while (pA !== pB) {
            pA = pA === null ? headB : pA.next;
            pB = pB === null ? headA : pB.next;
        }
        return pA;
    };

})();
